//
//  NetWorkTestVC.swift
//  SDKDebuggingDemo
//
//  Created by Qinsz on 2019/12/13.
//  Copyright © 2019年 QinszQSZ. All rights reserved.
//

import UIKit


class NetWorkTestVC: UIViewController {

    @IBOutlet weak var dialPreview: UIImageView!
    @IBOutlet weak var responseView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func clickPopButton(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    /// 获取AGPS信息
    @IBAction func clickAGPSInofs(_ sender: Any) {
        responseView.text = nil
        self.showTextInput(title: "输入AGPS类型", message: "1 – UBOX，2 – 索尼") { (text) in
            
            guard let type = Int(text) else {
                SVProgressHUD.showInfo(withStatus: "输入有误")
                return
            }
            
            SVProgressHUD.show(withStatus: nil)
            
            let task = KCTHTTPSession.share.AGPSInfoTask(fromProduct: false, appKey: TestAPPKey, secretKey: TestSecretKey,  type: type) {[weak self] (rspModel, error) -> (Void) in
                DispatchQueue.main.async {
                    SVProgressHUD.dismiss()
                    if let err = error {
                        self?.responseView.text = "获取AGPS失败:\(err)"
                        return
                    }
                    guard let rsp = rspModel?.yy_modelToJSONObject() else{
                        return
                    }
                    self?.responseView.text = "获取AGPS结果:\n\(rsp)"
                }
            }
            task?.resume()
        }
    }

    
    /// 获取表盘列表
    @IBAction func clickDialList(_ sender: Any) {
        
        self.responseView.text = nil
        
        self.showMultiTextInput(title: "获取表盘列表(测试服务器)", message: nil, lines: 2, placeHolders: ["设备特征","设备适配号"], contents: ["APAA8A4ADhAA6/AD","930"]) { (cs) in
            guard let num = Int(cs[1]) else {
                SVProgressHUD.showInfo(withStatus: NSLocalizedString("输入有误", comment: ""))
                return
            }
            
            let task = KCTHTTPSession.share.dialListTask(fromProduct: false, appKey: TestAPPKey, secretKey: TestSecretKey, deviceFeatureRaw: cs[0], adaptiveNumber: num) {[weak self] (js, error) -> (Void) in
                
                DispatchQueue.main.async {
                    if let `error` = error {
                        self?.responseView.text = "获取表盘列表失败:\(error)"
                        return
                    }
                    guard let `js` = js else{
                        self?.responseView.text = "获取表盘列表异常:ResponseModel为空"
                        return
                    }
                    self?.responseView.text = "获取表盘列表rsp:\(js.yy_modelToJSONObject() ?? "")"
                }
                
            }

            task?.resume()
            
        }

    }
    
    @IBAction func clickNewDialList(_ sender: Any) {
        
        self.responseView.text = nil
        
        self.showMultiTextInput(title: "获取表盘列表(测试服务器)", message: nil, lines: 3, placeHolders: ["设备特征","设备适配号","列表可选项。1字节的16进制数据。"], contents: ["APAA8A4ADhAA6/AD","930","0B"]) { (cs) in
            guard let num = Int(cs[1]) else {
                SVProgressHUD.showInfo(withStatus: NSLocalizedString("输入有误", comment: ""))
                return
            }

            guard let option = UInt8.init(cs[2],radix:16) else {
                SVProgressHUD.showInfo(withStatus: NSLocalizedString("Option输入有误", comment: ""))
                return
            }
            
            let task = KCTHTTPSession.share.optionalDialListTask(fromProduct: false, appKey: TestAPPKey, secretKey: TestSecretKey, deviceFeatureRaw: cs[0], adaptiveNumber: num, option: option) {[weak self] (js, error) -> (Void) in
                DispatchQueue.main.async {
                    if let `error` = error {
                        self?.responseView.text = "获取表盘列表失败:\(error)"
                        return
                    }
                    guard let `js` = js else{
                        self?.responseView.text = "获取表盘列表异常:ResponseModel为空"
                        return
                    }
                    self?.responseView.text = "获取表盘列表rsp:\(js.yy_modelToJSONObject() ?? "")"
                }
            }

            task?.resume()
            
        }
        
        
    }
    
    /// 获取表盘预览图文件
    @IBAction func clickDialPreview(_ sender: Any) {
        self.responseView.text = nil
        self.dialPreview.image = nil
        
        
        self.showMultiTextInput(title: "获取表盘预览图(测试服务器)", message: nil, lines: 1, placeHolders: ["表盘id"], contents: ["143360"]) { (cs) in
            
            guard let id = Int(cs[0]) else {
                SVProgressHUD.showInfo(withStatus: NSLocalizedString("输入有误", comment: ""))
                return
            }
            
            let task = KCTHTTPSession.share.dialPreviewTask(fromProduct: false,appKey: TestAPPKey, secretKey: TestSecretKey, dialPanelId: id,type: .normal) {[weak self] (image, error) -> (Void) in

                DispatchQueue.main.async {
                    
                    if let err = error {
                        IPrint("获取预览图error:\(err)")
                        self?.responseView.text = "获取表盘预览图失败:\(err)"
                        return
                    }
                    guard let img = image else{
                        self?.responseView.text = "获取表盘预览图异常:image为空"
                        return
                    }
                    self?.responseView.text = "获取表盘预览图成功"
                    self?.dialPreview.image = img
                }
            }
            
            task?.resume()
        }
    }
    
    
    @IBAction func clickFirmwareInfo(_ sender: Any) {
        
        self.responseView.text = nil
        
        self.showMultiTextInput(title: "获取固件信息(测试服务器)", message: nil, lines: 1, placeHolders: ["适配号"], contents: ["930"]) { (cs) in
            guard let num = Int(cs[0]) else {
                SVProgressHUD.showInfo(withStatus: "输入有误")
                return
            }
            
            let task = KCTHTTPSession.share.FirmwareInfoTask(fromProduct: false, appKey: TestAPPKey, secretKey: TestSecretKey,  adaptiveNum: num) { (infos, error) in
                DispatchQueue.main.async {
                    
                    if let err = error {
                        self.responseView.text = "获取固件信息失败失败:\(err)"
                        return
                    }
                    if let `infos` = infos {
                        self.responseView.text = "获取到固件信息:\n\(infos.yy_modelToJSONObject() ?? "")"
                    }else{
                        self.responseView.text = "线上没有固件信息"
                    }
                }
            }
            task?.resume()
        }
    }
    

}
